<?php
class GoodsCateModel extends MongoModel{
	var $table ='goods_cate';
	var $name = '商品分类';

	public static $VALUE_TYPE = array(
		'goods_cate_flags' 	=> 'int',
		'rank' 				=> 'int'
	);
	
	public static function getTree(){
		$cates = GoodsCateModel::getCates();
		$tree = new YTree($cates);
		return $tree;
	}
	
	public static function getCates(){
		$key = 'goods_cates_' . SITE_ID;
		$cates = Yii::app()->cache->get($key);
		if($cates === false){
			$mod = new GoodsCateModel();
			$condition = array('site_id'=>SITE_ID);
			$cates = $mod->find($condition, 'name,parent,rank,goods_cate_flags', null, null, array('rank'=>1));
			Yii::app()->cache->set($key, $cates);
		}
		return $cates;
	}
	
	public static function getOpts($has_null=false, $null_text = '--不限制--'){
		$tree = self::getTree();
		$opts = $tree->get_options('┊');
		if($has_null){
			return array_merge(array(''=>$null_text), $opts);
		}else{
			return $opts;
		}
	}
	
	/**
	 * 获取某一个
	 * @param unknown $cate_id
	 */
	private static function getCateFamilyList($cates, $cate_id){
		$cate_list = array();
		while (true){
			if(isset($cates[$cate_id])){
				$cate = $cates[$cate_id];
				$cate_list[] = $cate;
			}else{
				break;
			}
			
			if(isset($cate['parent']) && !empty($cate['parent'])){
				$cate_id = $cate['parent'];
			}else{
				break;
			}
		}
		
		return array_reverse($cate_list);
	}
	
	private static function inCateList($cate_list, $cate_id){
		if(empty($cate_list))return false;
		foreach ($cate_list as $cate){
			if($cate['_id'] == $cate_id) return true;
		}
		return false;
	}
	
	/**
	 * 获取某分类的路径列表，从root到当前分类
	 * @param array $cate_ids
	 */
	public static function getCateFamilyLists($cate_ids){
		$cates = self::getCates();
		$cate_list = array();
		
		//反向
		$cate_ids = array_reverse($cate_ids);
		$cur_cates = null;
		foreach ($cate_ids as $cate_id){
			if(empty($cate_id))continue;
			if($cur_cates != null){
				if(self::inCateList($cur_cates, $cate_id)){
					continue;
				}else{
					$cate_list[] = $cur_cates;
				}
			}
			
			$cur_cates = self::getCateFamilyList($cates, $cate_id);
		}
		if($cur_cates != null){
			$cate_list[] = $cur_cates;
		}
		
		return $cate_list;
	}
	
	public static function cleanCache(){
		$key = 'goods_cates_' . SITE_ID;
		Yii::app()->cache->delete($key);
	}
	
	/**
	 * 创建菜单
	 * @param unknown $goods_cate
	 * @param string $manage_model
	 * @return string
	 */
	public static function getUrl($goods_cate, $manage_model=false){
		if(is_string($goods_cate)){
			$goods_cate = GoodsCateModel::model()->get_by_key($goods_cate);
		}
		if($manage_model == false){
			return '/Goods/Cate/' . $goods_cate['_id'];
		}else{
			return '/manage/' . $manage_model . '/admin/' . $goods_cate['_id'];
		}
	}
	
	public static function getName($cate_ids, $path_split='.', $cate_split=',', $null_text=''){
		if(empty($cate_ids)){
			return $null_text;
		}
		
		$cate_list = self::getCateFamilyLists($cate_ids);
		if(empty($cate_list))
			return $null_text;
		
		$text = '';
		foreach ($cate_list as $cates){
			$cur_cate_text = '';
			foreach ($cates as $cate){
				if($cur_cate_text!=''){
					$cur_cate_text .= $path_split;
				}
				$cur_cate_text .= $cate['name'];
			}
			if(!empty($cur_cate_text)){
				if($text!=''){
					$text .= $cate_split;
				}
				$text .= $cur_cate_text;
			}
		}
		
		return $text;
	}
	
	public static function getCateFamilys($cate_ids, $path_split=' > ', $cate_split='<br>', $null_text='', $manage_model=false){
		if(empty($cate_ids)){
			return $null_text;
		}
		
		$cate_list = self::getCateFamilyLists($cate_ids);
		if(empty($cate_list))
			return $null_text;
		
		$text = '';
		foreach ($cate_list as $cates){
			$cur_cate_text = '';
			foreach ($cates as $cate){
				if($cur_cate_text!=''){
					$cur_cate_text .= $path_split;
				}
				$cur_cate_text .= CHtml::link($cate['name'], self::getUrl($cate, $manage_model));
			}
			if(!empty($cur_cate_text)){
				if($text!=''){
					$text .= $cate_split;
				}
				$text .= $cur_cate_text;
			}
		}
		
		return $text;
	}
}

?>